package com.yl.chainStore.services.impl.company

import com.baomidou.mybatisplus.core.toolkit.Wrappers
import com.yl.chainStore.entity.company.CompanySmsModel
import com.yl.chainStore.entity.company.CompanySmsModelType
import com.yl.chainStore.mapper.company.CompanySmsModelMapper
import com.yl.chainStore.mapper.company.CompanySmsModelTypeMapper
import com.yl.chainStore.pojo.vo.system.CompanySmsModelSelectVo
import com.yl.chainStore.services.company.ICompanySmsModelTypeService
import com.yl.common.ErrorCode
import com.yl.common.control.ResponseBody
import com.yl.common.control.returnError
import com.yl.common.control.returnSuccess
import org.springframework.stereotype.Service

@Service
class CompanySmsModelTypeServiceImpl(
	private val companySmsModelTypeMapper: CompanySmsModelTypeMapper,
	private val companySmsModelMapper: CompanySmsModelMapper
) : ICompanySmsModelTypeService {

	override fun getTypeById(id: Long): CompanySmsModelType {
		return companySmsModelTypeMapper.selectById(id)
	}

	override fun updateType(type: CompanySmsModelType): ResponseBody {
		if(type.typeName.isEmpty()){
			return returnError(ErrorCode.PARAM_EMPTY,"请输入类型名称")
		}
		companySmsModelTypeMapper.updateById(type)
		return returnSuccess()
	}

	override fun delType(id: Long): ResponseBody {
		val type =
			companySmsModelTypeMapper.selectById(id) ?: return returnError(ErrorCode.DATA_IS_NOT_FIND, "未找到类型")
		companySmsModelMapper.update(null, Wrappers.update<CompanySmsModel?>().set("enabled", 0).eq("type_id", id))

		type.enabled = 0
		companySmsModelTypeMapper.updateById(type)
		return returnSuccess()
	}

	override fun addModel(model: CompanySmsModel): ResponseBody {
		if(model.content.isEmpty()){
			return returnError(ErrorCode.PARAM_EMPTY,"请输入模板内容")
		}
		if(model.title.isEmpty()){
			return returnError(ErrorCode.PARAM_EMPTY,"请输入模板标题")
		}
		companySmsModelMapper.insert(model)
		return returnSuccess()
	}

	override fun updateModel(model: CompanySmsModel): ResponseBody {
		companySmsModelMapper.updateById(model)
		return returnSuccess()
	}

	override fun delModel(id: Long): ResponseBody {


		companySmsModelMapper.deleteById(id)
		return returnSuccess()
	}

	override fun getModelList(vo: CompanySmsModelSelectVo): List<CompanySmsModel> {
		return companySmsModelMapper.selectModelList(vo)
	}
}
